package com.zhongfl.crm.service.impl;

import com.zhongfl.crm.model.LxChannel;
import com.zhongfl.crm.model.LxUser;
import com.zhongfl.crm.service.LxUserService;
import com.zhongfl.crm.service.ResolveService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tech.heptagram.channel.api.common.AssertUtils;
import tech.heptagram.channel.api.enums.ChannelLevel;
import tech.heptagram.channel.api.enums.ChannelNature;
import tech.heptagram.channel.api.enums.ChannelSize;
import tech.heptagram.channel.api.model.Channel;

import javax.annotation.Resource;

/**
 * @author 木叶丸
 * @date 2019/3/5 15:26
 */
@Service
public class ResolveServiceImpl implements ResolveService {

    @Resource
    private LxUserService lxUserService;

    /**
     * 励销渠道转渠道
     *
     * @param lxChannel 励销渠道信息
     * @return 渠道信息
     */
    @Override
    public Channel resolve(LxChannel lxChannel) {
        AssertUtils.notNull(lxChannel, "励销渠道信息不存在，励销渠道转换为渠道失败！");
        LxUser lxUser = lxUserService.findByLxId(lxChannel.getUserId());
        AssertUtils.notNull(lxUser, "励销渠道所属用户: " + lxChannel.getUserName() + " 不在系统中，励销渠道转换为渠道失败！");
        Channel channel = new Channel();
        channel.setStaffName(lxUser.getStaffName());
        channel.setStaffCode(lxUser.getStaffCode());
        channel.setChannelName(lxChannel.getName());
        channel.setContactsName(lxChannel.getContactsName());
        channel.setContactsMobile(lxChannel.getContactsMobile());
        if (!ObjectUtils.isEmpty(lxChannel.getStaffSize())) {
            channel.setChannelSize(ChannelSize.getEnumBySizeId(lxChannel.getStaffSize()));
        }
        if (!StringUtils.isEmpty(lxChannel.getIndustry())) {
            channel.setChannelNature(ChannelNature.getEnumByNatureCode(lxChannel.getIndustry()));
        }
        if (!StringUtils.isEmpty(lxChannel.getCategory())) {
            channel.setChannelLevel(ChannelLevel.getEnumByLevelCode(lxChannel.getCategory()));
        }
        channel.setLxCustomerId(lxChannel.getLxId());
        channel.setAddress(lxChannel.getAddress());
        channel.setType("CUSTOMER");
        channel.setCreatedBy("BOSS系统");
        channel.setModifiedBy("BOSS系统");
        return channel;
    }
}
